package com.zhihuiweijian.demo.controller;

import com.zhihuiweijian.demo.model.Waste;
import com.zhihuiweijian.demo.service.WasteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("other")
@Api(tags={"其它统计操作接口"})
public class OtherController {
    @Autowired
    private WasteService wasteService;
    /**\
     * 格式化前台传来的日期数据
     * @param request
     * @param binder
     */
    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }


    @ApiOperation(value="所有医废列表及医废总量", notes="返回医废总数和详细列表",produces = "application/json")
    @GetMapping(value="/findAll")
    public Map<String, Object> findAll() {
        Map<String, Object> map = new HashMap();
        try {
            List<Waste> wastes = wasteService.findAll();
            map.put("num",wastes.size());
            map.put("data",wastes);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "查询失败!");
        }
        return map;
    }

    @ApiOperation(value="所有上报异常数据及详情列表", notes="上报的异常数据统计",produces = "application/json")
    @GetMapping(value="/findAllError")
    public Map<String, Object> findAllError() {
        Map<String, Object> map = new HashMap();
        try {
            List<Waste> wastes = wasteService.findAll();
            List<Waste> errorList = new ArrayList<>();

            for(Waste waste:wastes){
                //统计总共有多少上报异常的数据
                if(waste.getErrormanid()!=null){
                    errorList.add(waste);
                }
            }
            map.put("errorList",errorList);
            map.put("errorNum",errorList.size());
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }

    @ApiOperation(value="今天上报的异常信息", notes="",produces = "application/json")
    @GetMapping(value="/findError0fToday")
    public Map<String, Object> findErrorOfToday() {
        Map<String, Object> map = new HashMap();
        try {
            //获得当前时间
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            Calendar cal = Calendar.getInstance();
            String today = df.format(cal.getTime());
            cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - 1);
            String yestoday = df.format(cal.getTime());

            List<Waste> wastes = wasteService.findByDate(df.parse(yestoday),df.parse(today));
            List<Waste> errorList = new ArrayList<>();

            for(Waste waste:wastes){
                //统计总共有多少上报异常的数据
                if(waste.getErrormanid()!=null){
                    errorList.add(waste);
                }
            }
            map.put("errorList",errorList);
            map.put("errorNum",errorList.size());
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "查询失败!");
        }
        return map;
    }




    @ApiOperation(value="查找遗失数据", notes="距离打包已经超过2天时间，但仍未到达销毁厂也没有上报异常，则认为遗失",produces = "application/json")
    @GetMapping(value="/findAllMiss")
    public Map<String, Object> findAllMiss() {
        Map<String, Object> map = new HashMap();
        try {
            List<Waste> wastes = wasteService.findAll();
            List<Waste> missList = new ArrayList<>();
            for(Waste waste:wastes){
                //距离打包已经超过N长时间，但仍未到达销毁厂也没有上报异常，则认为遗失
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                Date date = new Date();
                long daysBetween=(date.getTime()-waste.getTime1().getTime()+1000000)/(60*60*24*1000);
                if(waste.getRuinmanid()==null && daysBetween>2 && waste.getErrormanid()==null){
                    missList.add(waste);
                }
            }

            map.put("num",missList.size());
            map.put("missRate",missList.size()/wastes.size());
            map.put("data",missList);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "查询失败!");
        }
        return map;
    }




    @ApiOperation(value="按照医院ID查询全部数据", notes="",produces = "application/json")
    @GetMapping(value="/findByHospitalId")
    public Map<String, Object> findByHospitalId(Integer id) {
        Map<String, Object> map = new HashMap();
        try {
            List<Waste> wastes = wasteService.findByHospitalId(id);
            map.put("data",wastes);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }

    @ApiOperation(value="按照日期查询全部数据", notes="",produces = "application/json")
    @GetMapping(value="/findByDate")
    public Map<String, Object> findByDate(Date d1,Date d2) {
        Map<String, Object> map = new HashMap();
        try {
            List<Waste> wastes = wasteService.findByDate(d1,d2);
            map.put("data",wastes);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }




}
